#!/bin/bash

LK_CODE_FILE="/var/lib/zentyal/.license"
LK_ID_FILE="/var/lib/zentyal/.license_id"
LK_TYPE_FILE="/var/lib/zentyal/.license_type"
LK_EXPIRATION_FILE="/var/lib/zentyal/.license_expiration"
LK_USERS_FILE="/var/lib/zentyal/.license_users"
LK_STATUS_FILE="/var/lib/zentyal/.license_status"
SERVER_HASH_FILE="/var/lib/zentyal/.server_uuid"
UCP_COMM_CLIENT_ID_FILE="/var/lib/zentyal/.ucp_comm_id"
UCP_COMM_CLIENT_SECRET_FILE="/var/lib/zentyal/.ucp_comm_secret"
RESPONSE_DATA_FILE_TMP=$(mktemp /tmp/XXXXXXX)

# Preparing request vars
LK=$(cat $LK_CODE_FILE)
RESPONSE_DATA_FILE_TMP=$(mktemp /tmp/XXXXXXX)

# Preparing the data with JSON encoding
JSON_STRING=$( jq -n \
                  --arg lk "$LK" \
                  '{
                      lk: $lk
                    }' 
            )

# Run the request to "check the license status"
REQUEST=$(/usr/bin/curl --silent -X POST -H "Content-Type: application/json" -d "$JSON_STRING" https://ucp.zentyal.com/api/lk/status -w "%{http_code}" -o $RESPONSE_DATA_FILE_TMP)

if [ $REQUEST -eq "200" ] || [ $REQUEST -eq "201" ]; then
	# Persisting LK data in a system file
	LK_DATA=$(cat $RESPONSE_DATA_FILE_TMP | jq -r ".data")

	# Gets the expiration date and persisting the expiration date in a system file
	LK_EXPIRATION=$(echo $LK_DATA | jq -r ".expiration_date")
	echo $LK_EXPIRATION > $LK_EXPIRATION_FILE

	# Gets the license type
	LK_TYPE=$(echo $LK_DATA | jq -r ".license_key_type.code")
	echo $LK_TYPE > $LK_TYPE_FILE

	# Gets the number of users for this license and persisting in a system file
	LK_USERS=$(echo $LK_DATA | jq -r ".users")
	echo $LK_USERS > $LK_USERS_FILE

	# Get the license key status id
	LK_STATUS=$(echo $LK_DATA | jq -r ".status")
	LK_STATUS_CODE=$(echo $LK_DATA | jq -r ".status.code")
	LK_STATUS_LABEL=$(echo $LK_DATA | jq -r ".status.label")
	echo $LK_STATUS > $LK_STATUS_FILE

	# Gets the lk hash
	SERVER_HASH=$(echo $LK_DATA | jq -r ".server_uuid")
	echo $SERVER_HASH > $SERVER_HASH_FILE
	
	# Get the license key ID
	LK_ID=$(echo $LK_DATA | jq -r ".id")

	# Get the oauth machine to machine client id
	UCP_CLIENT_ID=$(echo $LK_DATA | jq -r ".oaut_client.id")
	echo $UCP_CLIENT_ID > $UCP_COMM_CLIENT_ID_FILE

	# Get the ucp machine to machine secret
	UCP_CLIENT_SECRET=$(echo $LK_DATA | jq -r ".oaut_client.secret")
	echo $UCP_CLIENT_SECRET > $UCP_COMM_CLIENT_SECRET_FILE

	# Save the license key code
	LK_CODE=$(echo $LK_DATA | jq -r ".code")
	echo $LK_CODE > $LK_CODE_FILE

	# exit codes according to /usr/include/sysexits.h
	touch /var/lib/zentyal/.commercial-edition
	zs webadmin restart
	
	if [ $LK_STATUS_CODE == "L_ACTIVE" ]; then
		logger UCP[$$] INFO: License key $LK_CODE is valid.
		exit 0;

	elif [ $LK_STATUS_CODE == "L_INACTIVE" ]; then
		logger UCP[$$] INFO: License key $LK_CODE is $LK_STATUS_LABEL and is valid.
		exit 0;

	elif [ $LK_STATUS_CODE == "L_DUPLICATED" ]; then
		logger UCP[$$] INFO: License key $LK_CODE is $LK_STATUS_LABEL and is invalid.
		exit 77;

	elif [ $LK_STATUS_CODE == "L_DISABLED" ]; then
		logger UCP[$$] INFO: License key $LK_CODE is $LK_STATUS_LABEL and is invalid.
		exit 77;

	elif [ $LK_STATUS_CODE == "L_EXPIRED" ] && [ $LK_TYPE == "TR" ]; then
		logger UCP[$$] INFO: License key $LK_CODE is $LK_STATUS_LABEL and is invalid.
		exit 1;

	elif [ $LK_STATUS_CODE == "L_EXPIRED" ]; then
		logger UCP[$$] INFO: License key $LK_CODE is $LK_STATUS_LABEL and is invalid.
		exit 77;

	elif [ $LK_STATUS_CODE == "L_CANCELLED" ]; then
		logger UCP[$$] INFO: License key $LK_CODE is $LK_STATUS_LABEL and is invalid.
		exit 77;
	else
		logger UCP[$$] INFO: The license key status is unknow, nothing to do.
		exit 1;
	fi
else
	LK_ERR=$(cat $RESPONSE_DATA_FILE_TMP | jq -r ".data.error")
	RES_MSG=$(cat $RESPONSE_DATA_FILE_TMP | jq -r ".message")

	logger UCP[$$] ERROR: $RES_MSG.
	logger UCP[$$] ERROR: The connection with UCP cannot be established or the service is not available. $LK_ERR.
	exit 0;
fi